/*
 * @Author: humengchuan 531537052@qq.com
 * @Date: 2024-06-03 11:26:18
 * @LastEditors: humengchuan 531537052@qq.com
 * @LastEditTime: 2024-06-03 11:26:29
 * @FilePath: \project\record-work-time\uniCloud-aliyun\cloudfunctions\JWT\protected-resource.js
 * @Description: 
 */
// 受保护资源的中间件，验证JWT
const jwt = require('jwt-provider');
 
exports.main = async (event, context) => {
  const token = event.authorization; // 从请求头中获取token
  if (token) {
    try {
      const decoded = await jwt.verifyToken(token);
      // 使用解码后的JWT内容进行后续操作
      return {
        code: 200,
        data: decoded
      };
    } catch (err) {
      return {
        code: 401,
        errMsg: 'Token验证失败'
      };
    }
  } else {
    return {
      code: 401,
      errMsg: '未提供Token'
    };
  }
};